package com.aglhz.nature.adapter;

import java.util.List;

import com.aglhz.nature.R;
import com.aglhz.nature.ListView.MyListView;
import com.aglhz.nature.adapter.ColumnsListAdapter.ViewHolder;
import com.aglhz.nature.modle.item.classification.ListData;
import com.aglhz.nature.modle.item.classification.SecondClassificationListData;
import com.aglhz.nature.view.GoodDetailsAcitivty;
import com.aglhz.nature.view.ShopList2Activity;
import com.squareup.picasso.Picasso;

import android.content.Context;
import android.content.Intent;
import android.media.Image;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 二级分类listview adapter
 * 
 * @author Administrator
 * 
 */
public class SecondClassificationAdapter extends BaseAdapter {

	public List<SecondClassificationListData> mData;
	public Context context;
	private Object currentItemData;

	public SecondClassificationAdapter(
			List<SecondClassificationListData> mData, Context context) {
		super();
		this.mData = mData;
		this.context = context;
	}

	@Override
	public int getCount() {
		int tempCount = 0;
		for (int i = 0; i < mData.size(); i++) {
			tempCount++;
			for (int j = 0; j < mData.get(i).getLstData().size(); j++) {
				tempCount++;
			}
		}
		return tempCount;
	}
@Override
public int getViewTypeCount() {
	// TODO Auto-generated method stub
	return 2;
}
@Override
public int getItemViewType(int position) {
	// TODO Auto-generated method stub
	return getCurrentItemData(position).getType();
}
	@Override
	public Object getItem(int position) {
	
		return this.getCurrentItemData(position);
	}

	@Override
	public long getItemId(int position) {
		
		return 0;
	}


	private TempItem getCurrentItemData(int position) {
		
		int temp = 0;
		for (int i = 0; i < mData.size(); i++) {

			if (position == temp) {
				SecondClassificationListData data = mData.get(i);
				TempItem item = new TempItem();
				item.setTitle(mData.get(i).getTitle());
				item.setType(0);
				item.setLink(mData.get(i).getLink());
				item.setId(mData.get(i).getId());
				item.setItemData(data);
				
				return item;
			}
			temp++;

			for (int j = 0; j < mData.get(i).getLstData().size(); j++) {

				if (position == temp) {
					ListData data = mData.get(i).getLstData().get(j);
					TempItem item = new TempItem();
					item.setType(1);
					item.setGoodsName( mData.get(i).getLstData().get(j).getGoodsName());
					item.setIntroduction( mData.get(i).getLstData().get(j).getIntroduction());
					item.setPrice(mData.get(i).getLstData().get(j).getPrice());
					item.setGoodsImgCover(mData.get(i).getLstData().get(j).getGoodsImgCover());
					item.setLink(mData.get(i).getLstData().get(j).getLink());
					item.setId(mData.get(i).getLstData().get(j).getGoodsId());
					item.setItemData(data);
					return item;
				}
				temp++;
			}
		}
		return null;

	}

	@Override
	public View getView(final int position, View convertView, ViewGroup arg2) {
		
		View viewTitle;
		View viewImage;
		final TempItem item = this.getCurrentItemData(position);
		 ViewHolder viewHolder = null; 
		 ViewHolder1 viewHolder1 = null; 
		if(null==convertView){
			if(0==item.getType()){
				//tou
				viewHolder = new ViewHolder();
				viewTitle = LayoutInflater.from(context).inflate(
						R.layout.item_goodslist, null);
				viewHolder.cla_title = (TextView) viewTitle.findViewById(R.id.cla_title);
				viewHolder.cla_iv_more=(ImageView) viewTitle.findViewById(R.id.cla_iv_more);
				viewTitle.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						Intent intent =new Intent(context,ShopList2Activity.class);
						intent.putExtra("brandId",item.getId());
						Log.e("item.getLink()",item.getId());
						context.startActivity(intent);
						
					}
				});
				viewTitle.setTag(viewHolder);
				convertView=viewTitle;
			}else if(1==item.getType()){
				//image
				viewHolder1 = new ViewHolder1();
				 viewImage = LayoutInflater.from(context).inflate(
						R.layout.item_secclass_listview, null);
				 viewHolder1.goodsName=(TextView)  viewImage.findViewById(R.id.goodsName);
				 viewHolder1.relativelayout=(RelativeLayout) viewImage.findViewById(R.id.relativelayout);
				 viewHolder1.second_cla_tvMoney=(TextView) viewImage.findViewById(R.id.second_cla_tvMoney);				
				 viewHolder1.second_cla_iv=(ImageView)  viewImage.findViewById(R.id.second_cla_iv);
				 viewHolder1.second_cla_iv.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						Intent intent =new Intent(context,GoodDetailsAcitivty.class);
						intent.putExtra("detailLink",item.getLink());
						Log.e("item.getLink()",item.getLink());
						context.startActivity(intent);
						
					}
				});
				 viewImage.setTag(viewHolder1); 
				 convertView=viewImage;
			}
		}else{
			if(0==item.getType()){
				viewHolder = (ViewHolder) convertView.getTag(); 
			}else if(1==item.getType()){
				viewHolder1 =(ViewHolder1) convertView.getTag();
				}	
			}
		
			if(0==item.getType()){

				viewHolder.cla_title.setText(item.getTitle());
			}else if(1==item.getType()){
				
				viewHolder1.goodsName.setText(item.getGoodsName());
			
				viewHolder1.second_cla_tvMoney.setText("￥："+item.getPrice());
//				//设置图片
//				LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);  
//				lp.setMargins((int)(getScreenWidth()*0.025), (int)(getScreenWidth()*0.025), (int)(getScreenWidth()*0.025), (int)(getScreenWidth()*0.025));		
//				viewHolder1.second_cla_iv.setLayoutParams(lp); 
				  String imgUrl =item.getGoodsImgCover();
					//picasso加载网络图片
					   Picasso.with(context)  
					   .load(imgUrl) 
					   .resize(getScreenWidth(), getScreenWidth()*2/5)	    	               
					   .into(viewHolder1.second_cla_iv) ;
				
			}
			 
		return convertView;
	}

	class ViewHolder {

		public TextView cla_title;
		private ImageView cla_iv_more;
	}
	class ViewHolder1{
		
		public TextView goodsName;
		public RelativeLayout relativelayout;
		public TextView second_cla_tvMoney;
		public ImageView second_cla_iv;
	}

	/**
	 * 获取屏幕宽度
	 * 
	 * @return
	 */
	protected int getScreenWidth() {
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		return display.getWidth();
	}

	

}
